package Domain;

import java.io.File;
import java.util.*;

/**
 * @author gth854p
 */
public class ScannerGenerator {
	NFADiagram nfaDiagram;
	LinkedHashMap<String,String> tokenizedInput;
	LinkedHashMap<String,DefinedClass> lex;
	LinkedList<RuleCommand> regexStack;
	RegexParser regexParser;
	public ScannerGenerator(String s)
	{
		//scan input completely first to avoid memory leaks
		Scanner scan = new Scanner(s);
		tokenizedInput = new LinkedHashMap<String,String>();
		lex = new LinkedHashMap<String,DefinedClass>();
		regexStack = new LinkedList<RuleCommand>();
		while(scan.hasNextLine())
		{
			
			String line = scan.nextLine();
			line = line.trim();
			String[] tokens = line.split(" ", 2);
			tokenizedInput.put(tokens[0], tokens[1].trim());
		}
		scan.close();
	}
	
	/**
	 * UNION is the | character representing the union of two (sub-) regular expressions.
RE_CHAR is the union of two sets:
1)	the ASCII printable characters (see http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters) other than space, \, *, +, ?, |, [, ], (, ), ., ' and "
2)	escaped characters: \ (backslash space), \\, \*, \+, \?, \|, \[, \], \(, \), \., \' and \"
CLS_CHAR is the union of two sets:
1)	the ASCII printable characters (see http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters) other than \, ^, -, [ and ]
2)	escaped characters: \\, \^, \-, \[ and \]

	 */
	//determine if the first char is a bracket. if not, its a regex. if it is, its a defined class/character class
	public void createScanner()
	{
		regexParser = new RegexParser();
		
		
		
		
		for(String key : tokenizedInput.keySet())
		{
			String value = tokenizedInput.get(key);
			if(value.charAt(0)=='[')
			{
				DefinedClass dc = regexParser.getDefinedClass(value,lex);
				dc.setName(key);
				//System.out.println(dc.getCharSet().size());
				lex.put(key, dc);
				continue;
			}
			else
			{
				LinkedList<String> expansion = RegexParser.expandTokens(value, lex);
				System.out.println();
				Iterator<String> iter = expansion.iterator();
				while(iter.hasNext())
				{
					System.out.println(iter.next().toString());
				}
				System.out.println();
				RuleCommand root = null;
				root = regexParser.orderOps(expansion, lex,root);
				NFADiagram nfa = root.createNFA();
				nfa.getNfaEnd().setTerminal(true);
				//System.out.println(nfa.toString());
				/*RegexParser.SyntaxTreeNode root = regexParser.new SyntaxTreeNode();
				RegexParser.SyntaxTreeNode temp = regexParser.orderOps(expansion, lex,root);
				while(temp.getChild()!=null)
				{
					System.out.println(temp.getCommand().get(0).toString());
					temp = temp.getChild();
				}System.out.println(temp.getCommand().getFirst().toString());*/
			}
			/* now create the nfas with the regexStack lex*/
			//
			//int max = regexStack.size();
			//for(int i=0;i<max;i++)
			//	regexStack.pop().createNFA();
		}
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String test = "$DIGIT   [0-9] \n $NON-ZERO    [^0]  IN  $DIGIT \n $LETTER [a-zA-Z] \n $IDENTIFIER $LETTER($LETTER|$DIGIT)* "; // CHAR   [a-zA-Z] \n UPPER  [^a-z] IN CHAR \n IDENTIFIER     	LETTER (LETTER| DIGIT)* ";



		ScannerGenerator program = new ScannerGenerator(test);
		program.createScanner();
	}

}
